<template>
  <div class="loginStyle">
    <Logo></Logo>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button style="width: 100%" type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'VueYimutianResg' }"
      >没有账号？需要注册</router-link
    >
  </div>
</template>

<script>
import { Toast } from 'vant';
import { LoginAPI } from '../service/user';
import Logo from '../components/Logo.vue';
import { setToken } from '../tools/util';

export default {
  name: 'VueYimutianLogin',
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async onSubmit(values) {
      const res = await LoginAPI({
        userName: values.userName,
        password: values.password,
      });
      console.log(res);
      if (res.data.code === 1) {
        Toast.success('登录成功');
        setToken(res.data.data);
        this.$router.push('/user');
      } else {
        Toast.fail(res.data);
      }
    },
  },
  components: {
    Logo,
  },
};
</script>

<style scoped>
.loginStyle .van-form {
  margin-top: 20px;
}
</style>
